package com.yt.lxh.mq.service;

import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

@Service
public class RabbitService {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    //发送消息的方法
    //exchange交换机
    //routingKey路由
    //message消息
    public boolean sendMessage(String exchange, String routingKey, Object message){
        rabbitTemplate.convertAndSend(exchange, routingKey, message);
        return true;
    }

    //延迟消息
    //exchange交换机
    //routingKey路由
    //message消息
    //delay延迟时长 单位 ms 毫秒
    public boolean sendMessageByDelay(String exchange, String routingKey, Object message, Integer delay){
        rabbitTemplate.convertAndSend(exchange, routingKey, message, var -> {
            var.getMessageProperties().setDelay(delay);
            return var;
        });
        return true;
    }
}
